package com.cy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.study.entity.CourseEntity;
import com.cy.study.entity.MajorEntity;
import com.cy.study.entity.MediaEntity;
import com.cy.study.exception.BusinessException;
import com.cy.study.mapper.CourseMapper;
import com.cy.study.mapper.MajorMapper;
import com.cy.study.mapper.MediaMapper;
import com.cy.study.req.CourseAddReq;
import com.cy.study.req.CourseDeleteReq;
import com.cy.study.req.CoursePageReq;
import com.cy.study.req.UserCoursePageReq;
import com.cy.study.res.CourseDetailRes;
import com.cy.study.res.CoursePageRes;
import com.cy.study.res.UserCourseDetailRes;
import com.cy.study.res.UserCoursePageRes;
import com.cy.study.service.CourseService;
import com.cy.study.service.MajorService;
import com.cy.study.service.MediaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseEntity> implements CourseService {

    private final MajorMapper majorMapper;
    private final MediaMapper mediaMapper;

    @Override
    public Integer addCourse(CourseAddReq req) {
        //先检查课程编号是不是存在
        Integer courseNum = req.getCourseNum();
        if(courseNum==null){
            throw new BusinessException("请检查参数中课程编号是否存在");
        }
        QueryWrapper<MajorEntity> majorWrapper = new QueryWrapper<>();
        majorWrapper.lambda().like(MajorEntity::getCourseNums,req.getCourseNum());
        Long count = majorMapper.selectCount(majorWrapper);
        if(count==0){
            throw new BusinessException("课程编号不合法，请重试");
        }
        CourseEntity course = new CourseEntity();
        BeanUtils.copyProperties(req,course);
        course.setCreateTime(LocalDateTime.now());
        this.getBaseMapper().insert(course);
        return course.getId();
    }

    @Override
    public void deleteCourse(CourseDeleteReq req) {
        //先删除小节
        QueryWrapper<MediaEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MediaEntity::getCourseId,req.getCourseId());
        mediaMapper.delete(wrapper);
        //再删除课程
        this.removeById(req.getCourseId());
    }

    @Override
    public Page<CoursePageRes> pageCourse(CoursePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<CoursePageRes> res = this.getBaseMapper().pageCourse(req,page);
        return res;
    }

    @Override
    public CourseDetailRes detailCourse(Integer courseId) {
        CourseDetailRes res = new CourseDetailRes();
        CourseEntity course = this.getBaseMapper().selectById(courseId);
        res.setCourse(course);
        QueryWrapper<MediaEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MediaEntity::getCourseId,courseId)
                .orderByAsc(MediaEntity::getMediaNum);
        List<MediaEntity> medias = mediaMapper.selectList(wrapper);
        res.setMediaEntityList(medias);
        return res;
    }

    @Override
    public Page<UserCoursePageRes> userCoursePage(UserCoursePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<UserCoursePageRes> res = this.getBaseMapper().userCoursePage(req,page);
        return res;
    }

    @Override
    public UserCourseDetailRes userCourseDetail(Integer courseId) {
        UserCourseDetailRes res = new UserCourseDetailRes();
        CourseEntity course = getById(courseId);
        BeanUtils.copyProperties(course,res);
        return res;
    }
}
